import * as React from "react";
import {
  Image,
  StyleSheet,
  Text,
  View,
} from "react-native";
import { Card } from "react-native-shadow-cards";

//引入工具类
import app from "../utils/util";
import storage from '../utils/StorageUtil'


//获取数据
function reqdata(callback) {

    app.QQ("recipe_class?appkey=").then(res1 => {
      let num1 = res1.result.result[10].list[Math.floor((Math.random() * list.length))].classid;
      let num2 = Math.floor((Math.random() * 1000));
      app.QQ("byclass?classid=" + num1 + "&start=" + num2 + "&num=3" + "&appkey=").then(res2 => {
        storage.set("food_data_list",res2.result.result.list)

        callback(res2.result.result.list)
      });
    });
}

export default class list extends React.Component {
  constructor() {
    super();
    this.state = {
      list_data: [],
    };

  }

  componentDidMount() {
    storage.get("flag",(err,obj)=>{
      //判断第一次进入app初始化数据判断
      if (obj ==undefined) {
        storage.set("flag",true)
        reqdata(e=>{
          this.setState({
            list_data: e
          })
        })
      } else {
        //判断是不是同一天
        storage.get("data_time",(error, object) =>{
          if (new Date().toLocaleDateString() != object) {
            //每日刷新数据
            reqdata(e=>{
              this.setState({
                list_data:e
              })
            })
            //把今天的数据保存到本地
            storage.set("data_time",new Date().toLocaleDateString())
          } else {
            //获取本地的数据
            storage.get("food_data_list",(error, obj)=>{
              this.setState({
                list_data:obj
              })
            });
          }
        });

      }
    })

  }

  render() {

    return (
      <View style={styles.list_box} >
        {this.state.list_data.map( (list,index)  =>
          <Card cornerRadius={15} style={styles.list_card} key={index}>
            <View style={styles.list_view1}>
              <Image style={styles.list_img}
                     source={{ uri: list.pic }} />
              <View style={styles.list_view2}>
                <Text style={styles.list_title}>{list.name}</Text>
                <Text numberOfLines={2} escape={false}
                      style={styles.list_content}>{list.content}</Text>
              </View>
            </View>
          </Card>
        )}
      </View>
    );
  }
}
const styles = StyleSheet.create({
  list_box: {
    alignItems: "center",
    justifyContent: "center",
  },
  list_card: {
    height: 90,
    marginTop: 10,
    borderRadius: 10,
    width: app.w * 0.9,
    backgroundColor: "#fff",
  },
  list_view1: {
    flexDirection: "row",
    alignItems: "center",
    height: "100%",
    marginLeft: 10,
  },
  list_img: {
    width: 70,
    height: 70,
    marginRight: 5,
    borderRadius: 8,
  }, list_view2: {
    width: "80%",
    marginTop: 10,
  },
  list_title: {
    marginLeft: 10,
    marginBottom: 5,
    fontWeight: "bold",
    fontSize: 16,
    color: "rgba(24,24,24,0.8)",
  },
  list_content: {
    marginLeft: 10,
    marginRight: 10,
    fontSize: 12,
    color: "#7A7E81",
  },
});


